class SRKFWeapon extends KFWeapon
	abstract;

var       string         OwnerName;

replication
{
	reliable if(Role == ROLE_Authority)
		OwnerName;
}

simulated function MaxOutAmmo()
{
	local class<SRVeterancyTypes> CurrentPlayerVeterancy;
	local float AmmoAmountCoif;
	CurrentPlayerVeterancy = class<SRVeterancyTypes>(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill);
	AmmoAmountCoif=CurrentPlayerVeterancy.static.AddExtraAmmoFor(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo),  AmmoClass[0]);

	//Log("SRKFWeapon.MaxOutAmmo");
	if ( bNoAmmoInstances )
	{
		if ( AmmoClass[0] != None )
			AmmoCharge[0] = MaxAmmo(0) * AmmoAmountCoif;
		if ( (AmmoClass[1] != None) && (AmmoClass[0] != AmmoClass[1]) )
			AmmoCharge[1] = MaxAmmo(1) * AmmoAmountCoif;
		//Log("SRKFWeapon.MaxOutAmmo.bNoAmmoInstances");
		return;
	}
	if ( Ammo[0] != None )
		Ammo[0].AmmoAmount = Ammo[0].MaxAmmo * AmmoAmountCoif;
	if ( Ammo[1] != None )
		Ammo[1].AmmoAmount = Ammo[1].MaxAmmo * AmmoAmountCoif;
	//Log("SRKFWeapon.MaxOutAmmo");
}


function GiveTo(Pawn Other, optional Pickup Pickup)
{
	local SRKFWeapon W;
	Instigator = Other;
	
	Super.GiveTo(Other, Pickup);
	W = SRKFWeapon(Instigator.FindInventoryType(class));
	//Log("First Spread="@W@W.FireMode[0].Spread);
	if( W!=none )
	{
		W.OwnerName=Instigator.OwnerName;
		//Log("Second Spread="@W@W.FireMode[0].Spread);
		//Log("GiveTo Hurray"@W@W.OwnerName);
	}
}

function GiveAmmo(int m, WeaponPickup WP, bool bJustSpawned)
{
	local bool bJustSpawnedAmmo;
	local int addAmount, InitialAmount;

	UpdateMagCapacity(Instigator.PlayerReplicationInfo);
	//Log("GiveAmmo0 FireMode="$FireMode[m]@"FireMode[m].AmmoClass="$FireMode[m].AmmoClass@WP);
	if ( FireMode[m] != None && FireMode[m].AmmoClass != None )
	{
		Ammo[m] = Ammunition(Instigator.FindInventoryType(FireMode[m].AmmoClass));
		bJustSpawnedAmmo = false;
		//Log("GiveAmmo1 bNoAmmoInstances="$bNoAmmoInstances);
		if ( bNoAmmoInstances )
		{
			if ( (FireMode[m].AmmoClass == None) || ((m != 0) && (FireMode[m].AmmoClass == FireMode[0].AmmoClass)) )
			{
				//Log("GiveAmmo2");
				return;
			}
			InitialAmount = FireMode[m].AmmoClass.Default.InitialAmount;
			//Log("GiveAmmo3="$InitialAmount);
			if(WP!=none && WP.bThrown==true)
			{
				//Log("GiveAmmo4"@WP.AmmoAmount[m]);
				InitialAmount = WP.AmmoAmount[m];
			}
			else
			{
				// Other change - if not thrown, give the gun a full clip
				//Log("GiveAmmo5"@MagCapacity);
				MagAmmoRemaining = MagCapacity;
			}

			if ( Ammo[m] != None )
			{
				addamount = InitialAmount + Ammo[m].AmmoAmount;
				//Log("GiveAmmo6"@InitialAmount@Ammo[m].AmmoAmount@addamount);
				Ammo[m].Destroy();
			}
			else
			{
				//Log("GiveAmmo7"@InitialAmount);
				addAmount = InitialAmount;
			}
			
			//Log("GiveAmmo8"@addAmount@m);
			AddAmmo(addAmount,m);
		}
		else
		{
			//Log("GiveAmmo9"@Ammo[m]@FireMode[m].AmmoClass);
			if ( (Ammo[m] == None) && (FireMode[m].AmmoClass != None) )
			{
				Ammo[m] = Spawn(FireMode[m].AmmoClass, Instigator);
				Instigator.AddInventory(Ammo[m]);
				bJustSpawnedAmmo = true;
				//Log("GiveAmmo10"@Ammo[m]@bJustSpawnedAmmo);
			}
			else if ( (m == 0) || (FireMode[m].AmmoClass != FireMode[0].AmmoClass) )
			{
				bJustSpawnedAmmo = ( bJustSpawned || ((WP != None) && !WP.bWeaponStay) );
				//Log("GiveAmmo11"@WP.bWeaponStay@bJustSpawnedAmmo@WP);
			}
	  	      // and here is the modification for instanced ammo actors
			//Log("GiveAmmo12"@bJustSpawnedAmmo);
			if(WP!=none && WP.bThrown==true)
			{
				addAmount = WP.AmmoAmount[m];
				//Log("GiveAmmo13"@addAmount@WP.bThrown);
			}
			else if ( bJustSpawnedAmmo )
			{
				if (default.MagCapacity == 0)
				{
					addAmount = 0;  // prevent division by zero.
				}
				else
				{
					addAmount = Ammo[m].InitialAmount * (float(MagCapacity) / float(default.MagCapacity));
					//Log("GiveAmmo14"@addAmount@Ammo[m].InitialAmount@MagCapacity@default.MagCapacity);
				}
			}

			if ( 	WP != none && 
				(	WP.Class == class'BoomstickPickup' || 
					WP.Class == class'BoomstickWPickup' ||
					WP.Class == class'SBoomstickWPickup' 
				) && 
				m > 0 )
			{
				//Log("GiveAmmo15");
				return;
			}

			//Ammo[m].AddAmmo(addAmount);
			//Log("GiveAmmo16"@Ammo[m]@addAmount);
			AmmunitionAddAmmo(Ammo[m], addAmount, m);
			Ammo[m].GotoState('');
		}
	}
}

simulated function UpdateMagCapacity(PlayerReplicationInfo PRI)
{
	if ( KFPlayerReplicationInfo(PRI) != none && KFPlayerReplicationInfo(PRI).ClientVeteranSkill != none )
	{
		MagCapacity = default.MagCapacity * KFPlayerReplicationInfo(PRI).ClientVeteranSkill.Static.GetMagCapacityMod(KFPlayerReplicationInfo(PRI), self);
	}
	else
	{
		MagCapacity = default.MagCapacity;
	}
	////Log("MagCapacity="$MagCapacity@"default.MagCapacity="$default.MagCapacity);
}

function bool AddAmmo(int AmmoToAdd, int Mode)
{
	//Log("AddAmmo0");
	if ( bNoAmmoInstances )
	{
		if ( AmmoClass[0] == AmmoClass[mode] )
			mode = 0;
		if ( Level.GRI.WeaponBerserk > 1.0 )
		{
			AmmoCharge[mode] = MaxAmmo(Mode);
			//Log("AddAmmo1"@MaxAmmo(Mode));
		}
		else if ( AmmoCharge[mode] < MaxAmmo(mode) )
		{
			AmmoCharge[mode] = Min(MaxAmmo(mode), AmmoCharge[mode] + AmmoToAdd);
			//Log("AddAmmo2"@MaxAmmo(mode)@AmmoCharge[mode]@AmmoToAdd@AmmoCharge[mode]);
		}
		NetUpdateTime = Level.TimeSeconds - 1;
		return true;
	}
    if (Ammo[Mode] != None)
	{
		//Log("AddAmmo3");
		return AmmunitionAddAmmo(Ammo[Mode], AmmoToAdd, Mode);
	}
}

//
function bool AmmunitionAddAmmo(Ammunition Amm, int AmmoToAdd, int Mode)
{
	local class<SRVeterancyTypes> CurrentPlayerVeterancy;
	local float AmmoAmountCoif;
	CurrentPlayerVeterancy = class<SRVeterancyTypes>(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill);
	AmmoAmountCoif=CurrentPlayerVeterancy.static.AddExtraAmmoFor(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo),  FireMode[Mode].AmmoClass);

	//Log("AmmunitionAddAmmo");
	if ( Level.GRI.WeaponBerserk > 1.0 )
	{
		//Log("AmmunitionAddAmmo1");
		Amm.AmmoAmount = Amm.MaxAmmo * AmmoAmountCoif;
	}
	else if ( Amm.AmmoAmount < Amm.MaxAmmo * AmmoAmountCoif )
	{
		//Log("AmmunitionAddAmmo2");
		if(AmmoToAdd>0)
		{
			Amm.AmmoAmount = Min(Amm.MaxAmmo * AmmoAmountCoif, Amm.AmmoAmount + AmmoToAdd);
		}
	}
	NetUpdateTime = Level.TimeSeconds - 1;
	//Log(Amm@Amm.AmmoAmount@Amm.MaxAmmo * AmmoAmountCoif@AmmoToAdd);
	return true;
}

defaultproperties
{
     OwnerName="Unknown33554432"
}
